package com.resolution.dietaryassessment.view.mydietary.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.view.mydietary.bean.FoodTotalCalories;
import com.resolution.dietaryassessment.view.mydietary.customview.RoundImageView;
import com.resolution.dietaryassessment.view.mydietary.fragment.DelClick;

import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;
import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.DAIRY_STATUS;
import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.MEAT_STATUS;


/**
 * @author By Resolusion
 * @Title: 餐次食物容器Recycler
 * @Date: 2020/4/15
 * @Time: 21:48
 * @email Res01ution@foxmail.com
 */
public class RecyclerSimpleAdapter extends RecyclerView.Adapter<RecyclerSimpleAdapter.MyViewHolder> {

    private List<FoodTotalCalories> mFoodTotalCaloriesList;
    private DelClick delClick;
    private final static FoodTotalCalories[] init = {new FoodTotalCalories(R.mipmap.add, null, 0,-1)};
    //默认未点击下的
    private List<FoodTotalCalories> initIDs = new ArrayList<>();

    // 两次点击按钮之间的点击间隔不能少于500毫秒
    private static final int MIN_CLICK_DELAY_TIME = 500;
    private static long lastClickTime;

    public RecyclerSimpleAdapter(List<FoodTotalCalories> foodTotalCaloriesList) {
        mFoodTotalCaloriesList = foodTotalCaloriesList;
        initIDs.add(init[0]);
        initNullData();
    }

    public void setDelClick(DelClick delClick) {
        this.delClick = delClick;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        View mFoodCardView;
        RoundImageView imageView;
        ImageView cancel_btn;

        MyViewHolder(View view) {
            super(view);
            mFoodCardView = view;
            imageView = view.findViewById(R.id.iv_img);
            cancel_btn = view.findViewById(R.id.cancel_btn);
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_upload_image, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.cancel_btn.setVisibility(View.GONE);

        int resId = mFoodTotalCaloriesList.get(position).getRes_id();
        if (resId != R.mipmap.add) {
            holder.cancel_btn.setVisibility(View.VISIBLE);
            //view置于顶层
            holder.cancel_btn.bringToFront();
            holder.cancel_btn.setAlpha(168);
        }
        holder.imageView.setImageResource(resId);

        holder.cancel_btn.setOnClickListener(v -> {
            if (isFastClick()) {
                Log.d(TAG, "onBindViewHolder: " + position);
                if (mFoodTotalCaloriesList.get(position).getState() == MEAT_STATUS ||
                        mFoodTotalCaloriesList.get(position).getState() == DAIRY_STATUS) {
                    delClick.delHQProtein(position);
                }
                delClick.delClick(position);
                delClick.elementListener(position);

                mFoodTotalCaloriesList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, getItemCount());
                Log.d(TAG, "onBindViewHolder: " + mFoodTotalCaloriesList);
                initNullData();
            }
        });
    }

    /**
     * 防止用户快速点击而造成的数组越界
     */
    private static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    @Override
    public int getItemCount() {
        if (mFoodTotalCaloriesList != null) {
            return mFoodTotalCaloriesList.size();
        } else {
            return 0;
        }
    }

    private void initNullData() {
        if (mFoodTotalCaloriesList.size() == 0) {
            mFoodTotalCaloriesList = initIDs;
        }
    }

    public List<FoodTotalCalories> getFoodTotalCaloriesList() {
        return mFoodTotalCaloriesList;
    }

    public void setFoodTotalCaloriesList(List<FoodTotalCalories> mFoodTotalCaloriesList) {
        this.mFoodTotalCaloriesList = mFoodTotalCaloriesList;
    }
}
